import Layout from '@/layout/Index.vue'

// 菜单项数据的类型定义
interface Menu {
  menuId: number;
  menuName: string;
  parentId: number;
  menuType: string;
  path: string;
  component: string;
  icon: string;
  hidden: string;
  cache: string;
  // 其他属性
}

// 定义一个函数来将菜单数据转换为需要的路由配置
export function generateRoutes(menuItems: Menu[], parentId: number): any[] {
  const routes: any[] = [];

  menuItems.forEach(menuItem => {
    if (menuItem.parentId === parentId) {
      const route: any = {
        path: `/${menuItem.path}`,
        component: Layout,
        redirect: `/${menuItem.path}/${menuItems[0].path}`,
        meta: {
          title: menuItem.menuName,
          icon: menuItem.icon,
          hidden: menuItem.hidden,
        },
        children: generateRoutes(menuItems, menuItem.menuId),
      };

      if (menuItem.menuType === "2") {
        // 二级菜单
        route.component = () => import(`@/views/${menuItem.component}.vue`);
        route.name = menuItem.menuName;
        route.meta.title = menuItem.menuName;
        delete route.children; // 移除没有子路由的菜单项的children属性
      }

      routes.push(route);
    }
  });

  return routes;
}